﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HW3
{
   
    class CalculateTables
    {
        private Prop prop;

        public CalculateTables(Prop prop)
        {
            this.prop = prop;
            InitialDataTable1();
            FillDataTable1();
            CalculateTable1();
            InitialDataTable2();
            FillDataTable2();
            CalculateTable2();
        }

        #region  DataTable1

        private List<string> globalColumnsListT1 = new List<string>();
        private Hashtable columnsMappingT1 = new Hashtable();

        /// <summary>
        ///  Initial DataTable1 Columns Names and Rows
        /// </summary>
        private void InitialDataTable1()
        {
            prop.DataTable1 = new DataTable();
            CreateColumnsT1("Amount");
            CreateColumnsT1("Frequancy");
            CreateColumnsT1("Probability");
            CreateColumnsT1("Integral Probability");
            CreateColumnsT1("Lower");
            CreateColumnsT1("Upper");

            for (int i = 0; i < globalColumnsListT1.Count; i++)
            {
                prop.DataTable1.Columns.Add(new DataColumn(globalColumnsListT1[i], (Type)columnsMappingT1[globalColumnsListT1[i]]));
            }

            for (int i = 0; i < prop.Table1.Length / 2; i++)
            {
                prop.DataTable1.Rows.Add(new object[] { });
            }

        }

        /// <summary>
        /// Fill DataTable1 from Text File Table1
        /// </summary>
        private void FillDataTable1()
        {
            string amount = globalColumnsListT1[0];
            string frequancy = globalColumnsListT1[1];
            for (int i = 0, j= 0 ; i < prop.Table1.Length / 2; i++, j +=2)
            {
                prop.DataTable1.Rows[i][amount] = prop.Table1[j];
                prop.DataTable1.Rows[i][frequancy] = prop.Table1[j+1];
            }
        }

        /// <summary>
        /// Calculated DataTable1 
        /// </summary>
        private void CalculateTable1()
        {
            // length Rows
            int length = prop.Table1.Length/2;
            double sumFrequancyT1 = 0;
            // Columns Name From List
            string frequancy = globalColumnsListT1[1];
            string probability = globalColumnsListT1[2];
            string integralProbability = globalColumnsListT1[3];
            string lower = globalColumnsListT1[4];
            string upper = globalColumnsListT1[5];

            // Sum Frequancy
            for (int i = 0; i < length; i++)
            {
                sumFrequancyT1 += (double) prop.DataTable1.Rows[i][frequancy];
            }

            // Calculated probability Columns
            for (int i = 0; i < length; i++)
            {
                prop.DataTable1.Rows[i][probability] = (double) prop.DataTable1.Rows[i][frequancy]/sumFrequancyT1;
            }

            // Calculated Integral Probability Columns
            for (int i = 0; i < length; i++)
            {
                if (i == 0)
                    prop.DataTable1.Rows[i][integralProbability] = (double) prop.DataTable1.Rows[i][probability];
                else
                    prop.DataTable1.Rows[i][integralProbability] =
                        (double) prop.DataTable1.Rows[i - 1][integralProbability] +
                        (double) prop.DataTable1.Rows[i][probability];
            }

            // Calculated Upper Columns
            for (int i = 0; i < length; i++)
            {
                prop.DataTable1.Rows[i][upper] = (double) prop.DataTable1.Rows[i][integralProbability]*100;
            }

            // Calculated Lower Columns 
            for (int i = 0; i < length; i++)
            {
                if (i == 0) prop.DataTable1.Rows[i][lower] = 1;
                else prop.DataTable1.Rows[i][lower] = (double) prop.DataTable1.Rows[i - 1][upper] + 1;
            }
        }

        #endregion  DataTable1

        #region Help DataTable1

        /// <summary>
        /// Ctreated Columns Names
        /// </summary>
        /// <param name="columnName"></param>
        private void CreateColumnsT1(string columnName)
        {
            globalColumnsListT1.Add(columnName);
            columnsMappingT1.Add(columnName, (typeof(double)));
        }

        #endregion Help DataTable1

        #region  DataTable2

        private List<string> globalColumnsListT2 = new List<string>();
        private Hashtable columnsMappingT2 = new Hashtable();

        /// <summary>
        ///  Initial DataTable2 Columns Names and Rows
        /// </summary>
        private void InitialDataTable2()
        {
            prop.DataTable2 = new DataTable();
            CreateColumnsT2("Order Reception");
            CreateColumnsT2("Probability");
            CreateColumnsT2("Integral Probability");
            CreateColumnsT2("Lower");
            CreateColumnsT2("Upper");

            for (int i = 0; i < globalColumnsListT2.Count; i++)
            {
                prop.DataTable2.Columns.Add(new DataColumn(globalColumnsListT2[i], (Type)columnsMappingT2[globalColumnsListT2[i]]));
            }

            for (int i = 0; i < prop.Table2.Length / 2; i++)
            {
                prop.DataTable2.Rows.Add(new object[] { });
            }

        }

        /// <summary>
        /// Fill DataTable2 from Text File Table2
        /// </summary>
        private void FillDataTable2()
        {
            string orderReception = globalColumnsListT2[0];
            string probability = globalColumnsListT2[1];
            for (int i = 0, j = 0; i < prop.Table2.Length / 2; i++, j += 2)
            {
                prop.DataTable2.Rows[i][orderReception] = prop.Table2[j];
                prop.DataTable2.Rows[i][probability] = prop.Table2[j + 1];
            }
        }

        /// <summary>
        /// Calculated DataTable2
        /// </summary>
        private void CalculateTable2()
        {
            // length Rows
            int length = prop.Table2.Length / 2;
            double sumFrequancyT2 = 0;
            // Columns Name From List
            string probability = globalColumnsListT2[1];
            string integralProbability = globalColumnsListT2[2];
            string lower = globalColumnsListT2[3];
            string upper = globalColumnsListT2[4];

            // Sum Frequancy
            for (int i = 0; i < length; i++)
            {
                sumFrequancyT2 += (double)prop.DataTable2.Rows[i][probability];
            }

            // Calculated Integral Probability Columns
            for (int i = 0; i < length; i++)
            {
                if (i == 0)
                    prop.DataTable2.Rows[i][integralProbability] = (double)prop.DataTable2.Rows[i][probability];
                else
                    prop.DataTable2.Rows[i][integralProbability] =
                        (double)prop.DataTable2.Rows[i - 1][integralProbability] +
                        (double)prop.DataTable2.Rows[i][probability];
            }

            // Calculated Upper Columns
            for (int i = 0; i < length; i++)
            {
                prop.DataTable2.Rows[i][upper] = (double)prop.DataTable2.Rows[i][integralProbability] * 100;
            }

            // Calculated Lower Columns 
            for (int i = 0; i < length; i++)
            {
                if (i == 0) prop.DataTable2.Rows[i][lower] = 1;
                else prop.DataTable2.Rows[i][lower] = (double)prop.DataTable2.Rows[i - 1][upper] + 1;
            }
        }

        #endregion  DataTable2

        #region Help DataTable2

        /// <summary>
        /// Ctreated Columns Names
        /// </summary>
        /// <param name="columnName"></param>
        private void CreateColumnsT2(string columnName)
        {
            globalColumnsListT2.Add(columnName);
            columnsMappingT2.Add(columnName, (typeof(double)));
        }

        #endregion Help DataTable2

    }
}
